<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class cart extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
	}
	
	function index()
	{
		$this->menu_main();	
	}
	
	function menu_main()
	{    
                //check if the user is logged in from the session cockies and if yes will load the member page
		if ($this->session->userdata('c_logged_in'))
		{
			
			// get the queue list for cart from get queue function
			list ($cartlist) = $this->get_cart();
			
			// check if there are movies in the queue list and if TRUE a table will be generated and if FALSE nothing will be generated
			if($cartlist != FALSE)
			{
				// generate the table with the two previous commands and the resulted queue of movies  and assign this generated table to an array
				$full_table['items'] = $cartlist->result_array();
			}
			else
			{
				// assign FALSE the movies array so the code can distinguish between if we have table or not and print the error if not
				$full_table['items'] = FALSE;
			}
                        
			// load the queue viewing page to the member after inputting the full table with movies and games queues tables t print to member
			$this->load->view('view_cart',$full_table);
			// run the search function to view the search field in the member page
			//$this->search();
		}
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}        
		
	}
        
        
        function update_cart()
        {
            
           echo "inside update_cart";
        }
        
      
	function shipped_items()
        {
            //$shipped = $this->shipped_list();
        }
        
        function remove_cart()
        {
          //check if the user is logged in from the session cockies and if yes will load the member page
		if ($this->session->userdata('c_logged_in'))
		{
                    // get the customer id from the session cockie to be used as input to delete the movie or game to his/her 
                    $cus_id = $this->session->userdata('cus_id');
                    $order_id = $this->uri->segment(3);
                    // delete the dvd from the customer queue using cusrtomer id, film id or game id, dvd type and priority
                    $this->model_cart->delete_cart($cus_id,$order_id);

                    // go back to the index to view the member main page after deleting the movie to the queue
                    $this->index();
                }
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}
        }
        
        
         function remove_all()
        {
          //check if the user is logged in from the session cockies and if yes will load the member page
		if ($this->session->userdata('c_logged_in'))
		{
                    // get the customer id from the session cockie to be used as input to delete the movie or game to his/her 
                    $cus_id = $this->session->userdata('cus_id');
                 
                    // delete the dvd from the customer queue using cusrtomer id, film id or game id, dvd type and priority
                    $this->model_cart->delete_all_cart($cus_id);

                    // go back to the index to view the member main page after deleting the movie to the queue
                    $this->index();
                }
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}
        }
        
        function logout()
	{
		/* to destroy the seesion and logout*/
		$this->session->sess_destroy();
                redirect('yogette/cart');
	}
        
        function emp_checkout()
        {
            if ($this->session->userdata('e_logged_in'))
            {
                
                   $emp_id = $this->session->userdata('emp_id');
                   $emp_email = $this->session->userdata('emp_email');
                   // Update finsihed for reservation flag and insert in history
                   
                   $order_id = $this->uri->segment(3);
                   
                   //$res_queue['reserv'] = $this->model_cart->get_cart_item($order_id);

                  // if ($res_queue['reserv'])
                   //{
                   //    $result = $this->model_reserve->paid_reservation($cus_id , $res_queue);
                   //}

                   $cart_queue = $this->model_cart->get_cart_item($order_id);
                   //$res_queue['items'] = $this->model_cart->get_cart($cus_id);
                   if ($cart_queue != FALSE)//$res_queue['items']
                   {
                       // adding libility
                       $res_queue['items'] = $cart_queue->result();
                       $result = $this->model_user->add_libility_from_emp($emp_id , $emp_email , $res_queue);
                   }
                   else
                    {
                       // assign FALSE the movies array so the code can distinguish between if we have table or not and print the error if not
                       $res_queue['items'] = FALSE;
                    }
                    // Update finsihed for reservation flag
                   if ($result)
                       {
                           redirect('yogette/member');
                       }
                       else
                       {
                           redirect('yogette/error');

                       }      
                
            }
            else
            {
		// the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user/login');
            }     
        }
        
        function emp_reject()
        {
            if ($this->session->userdata('e_logged_in'))
            {
                   //processes their input login the user
                   extract($_POST);
                   $cus_id = $this->input->post('cus_id');
                   $cus_email = $this->input->post('cus_email');
                   
                   $emp_id = $this->session->userdata('emp_id');
                   $email = $this->session->userdata('emp_email');
                   $this->form_validation->set_rules('spcialevent_desc', 'Special Description', 'required|trim|max_length[200]|min_length[5]|xss_clean');
                   $this->form_validation->set_rules('spcialevent', 'Special', 'required');
                   
                 if ($this->form_validation->run() == FALSE) {
                      
                       
			
			// get the search array result from the seearch model from the DB by supplying the dvd type (games or movies) and the search field
                      $search_results = $this->model_search->search_special_customer($cus_id);                 
                      $search_table['items'] = $search_results;
                      $search_table['flag'] = True;
                      $this->load->view('view_search_special',$search_table);
                    }
                    else 
                    {
                        $this->load->model('Model_cart', 'cart');
                        $order_id = $this->input->post('spcialevent');
                        $special_desc = $this->input->post('spcialevent_desc');
                        
                       

                        $this->cart->emp_update_order_special($order_id , $cus_email , $special_desc);
                        
                        $status = $search_results = $this->model_search->search_special_customer($cus_id);
                        if ($status)
                        {
                            $search_table['items'] = $search_results;
                            $search_table['flag'] = True;
                            $this->load->view('view_search_special',$search_table);
                        }
                        else
                         {
                            redirect('yogette/error');
                         }
                     }                
            }
            else
            {
		// the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user/login');
            }     
        }
        
        function checkout()
        {
            //check if there is any item in his Queue
           if ($this->session->userdata('c_logged_in'))
            { 
               $cus_id = $this->session->userdata('cus_id');
               
                // delete the dvd from the customer queue using cusrtomer id, film id or game id, dvd type and priority
                //$order_list = $this->model_cart->get_cart($cus_id);

                list ($cartlist) = array ($this->model_cart->get_cart($cus_id));
                
                if($cartlist != FALSE)
			{
				// generate the table with the two previous commands and the resulted queue of movies  and assign this generated table to an array
				$res_queue['items'] = $cartlist->result_array();
			}
			else
			{
				// assign FALSE the movies array so the code can distinguish between if we have table or not and print the error if not
				$res_queue['items'] = FALSE;
			}
                
                $res_queue['reserv'] = $this->model_reserve->get_my_reservations($cus_id);
                //check if there is any reservation in his Queue

                $this->load->view('view_checkout',$res_queue);
            }
            else
            {
		// the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user/login');
            }
            
        }
        
     
        function update_order_special()
        {
            if ($this->session->userdata('c_logged_in'))
            {
                   $cus_id = $this->session->userdata('cus_id');
                   $email = $this->session->userdata('cus_email');
                   $this->form_validation->set_rules('spcialevent_desc', 'Special Description', 'required|trim|max_length[200]|min_length[5]|xss_clean');
                   $this->form_validation->set_rules('spcialevent', 'Special', 'required');
                   
                 if ($this->form_validation->run() == FALSE) {
                      $this->index();
                }
                else 
                {
                    $this->load->model('Model_cart', 'cart');
                    $order_id = $this->input->post('spcialevent');
                    $special_desc = $this->input->post('spcialevent_desc');
                    
                    $this->cart->update_order_special($order_id , $email , $special_desc);
                    $this->index();
                 }
            }
            else
            {
                //redirect('yogette/cart');
            }  
        }
        
        function get_cart() // viewcart function zoms
	{
            //check if the user is logged in from the session cockies and if yes will load the member page
		if ($this->session->userdata('c_logged_in'))
		{
                    //get the the customer id from the session cockie
                    $cus_id = $this->session->userdata('cus_id');
                    $cus_email = $this->session->userdata('cus_email');
                    $cus_username = $this->session->userdata('cus_username');
                    
                    $this->load->model('Model_cart', 'cart');
                    //get the customer movies queue, 1 as an input will means movies
                    $cart_queue = $this->cart->get_cart($cus_id);

                    // return both the movies and games queue list
                    return array ($cart_queue);

                }
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}
	}
 
        function search()
	{
		
		// this rule to check the search field validaty as required 
		$this->form_validation->set_rules('search_field', 'Search field', 'required');
		
		// check the validation of the filled form  if it is false the user will be viewing  to search field again
		if ($this->form_validation->run() == FALSE)
		{
			//loading the view of search field 
			$this->load->view('view_search');
		}
		else
		{
			
			
			//processes their input login the user
			extract($_POST);
			
			// get the search array result from the seearch model from the DB by supplying the dvd type (games or movies) and the search field
			$search_results = $this->model_search->model_search($search_field);
			
			// run another function to generate the table and output to the member the list result by providing the search array result from the DB and and the dvd type either movie or game 
			$this->search_list($search_results);
			
		}	
	}
        
        function search_list($search_results, $dvd_type)
	{
	
            // assign to the search table the dvd type to be 1 "movie" so passing this variable will help to indicate the type of results either movies or games
          

            // check if the array of results is not empty before gemerating the table to be able to print the proper error
            //if it is empty will assign FALSE for both games and movies 
            if ($search_results != FALSE)
            {
                    // clearing the table from any values to be sure that it is empty before adding the inputs
                    $this->table->clear();

                    // add Null to the empty culomns
                    $this->table->set_empty("&nbsp;");

                    // adding the heading of the table to be specific for movies
                    $this->table->set_heading('Movie ID', 'Movie Title', 'Genre ID', 'Click to Add');

                    // run a for loop for each row in the result array to add the link to add a specific movie from the resulted array to the queue
                    foreach ($search_results as $search_results)
                    {

                            //adding all required fields in addition to the add link to the queue
                            $this->table->add_row($search_results['film_id'], $search_results['film_title'], $search_results['genre_id'], anchor('member/add_queue/'.$search_results['film_id']. '/' .$search_table['dvd_type'],'Add', array('class' => 'add')));
                    }

                    // generate the table with all required fields for movies
                    $search_table['movies'] = $this->table->generate();

                    // assigning FALSE to games array to be sure not to print to the member and provide the proper error
                    $search_table['games'] = FALSE;

            }
            else
            {

                    // assigning False to both movies and games arrays to provide the proper error
                    $search_table['movies'] = FALSE;
                    $search_table['games'] = FALSE;

            }

            // view the the both searching results tables for either movies or games by passing the generated search table arrays with the dvd type inside this array 
            $this->load->view('view_search_results',$search_table);

            // view the search field again after getting the results so the customer can search again if he/she does not want to add this movie or game to the queue
            $this->load->view('view_search');

	}
        
        
       
        
}